package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.PlanManageProgressListAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.PlanManage;
import com.isunland.managesystem.entity.PlanManageProgress;
import com.isunland.managesystem.entity.PlanManageProgressOriginal;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/25 11:22
 */
public class PlanManageProgressListFragment extends BaseListFragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.PlanManageProgressListFragment.EXTRA_CONTENT";
  private ArrayList<PlanManageProgress> mlist;
  private PlanManage mContent;
  private final int REQUST_DETAIL = 0;
  private int mPage;
  private PlanManageProgressListAdapter mAdapter;
  private CurrentUser mUser;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContent = (PlanManage) getArguments().get(EXTRA_CONTENT);
    setTitleCustom(mContent.getPlantitle());
    mUser = CurrentUser.newInstance(getActivity());
    mlist = new ArrayList<>();
    mPage = 1;
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //添加进展
        if (mContent.getExcjobno().contains(mUser.getJobNumber()) || mContent.getCorjobno()
            .contains(mUser.getJobNumber())) {
          Intent add = new Intent(getActivity(), PlanManageProgressDetailActivity.class);
          add.putExtra(PlanManageProgressDetailFragment.EXTRA_VALUE, mContent);
          startActivityForResult(add, REQUST_DETAIL);
        } else {
          ToastUtil.show("只有负责人和参与人可以登记进展!");
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_PLAN_PROGRESS_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();
    params.put("memberCode", mUser.getMemberCode());
    params.put("mainid", mContent.getId());
    params.put("page", mPage + "");
    params.put("pageSize", "20");
    return params.getParams();
  }

  @Override public void resolveResponse(String json) {
    PlanManageProgressOriginal original =
        new Gson().fromJson(json, PlanManageProgressOriginal.class);
    ArrayList<PlanManageProgress> rows = original.getRows();
    if (rows != null && Base.SUCCESS == original.getResult()) {
      if (mPage == 1) {
        mlist.clear();
      }
      mlist.addAll(rows);
      if (mAdapter == null) {
        mAdapter = new PlanManageProgressListAdapter(getActivity(), mlist);
        setListAdapter(mAdapter);
        return;
      }
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    if (PullToRefreshBase.Mode.PULL_FROM_START == refreshView.getCurrentMode()) {
      mPage = 1;
    }
    if (PullToRefreshBase.Mode.PULL_FROM_END == refreshView.getCurrentMode()) {
      mPage++;
    }
    volleyPost();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    PlanManageProgress progress = mlist.get(position - 1);
    Intent i = new Intent(getActivity(), PlanManageProgressDetailActivity.class);
    i.putExtra(PlanManageProgressDetailFragment.EXTRA_VALUE, mContent);
    i.putExtra(PlanManageProgressDetailFragment.EXTRA_PROGRESSVALUE, progress);
    startActivityForResult(i, REQUST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUST_DETAIL) {
      mPage = 1;
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
      volleyPost();
      mPullRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
    }
  }

  public static PlanManageProgressListFragment newInstance(PlanManage content) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    PlanManageProgressListFragment fragment = new PlanManageProgressListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
